//
//  CommissionPayModeController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/3/20.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "CommissionPayModeController.h"
#import "CommissionAliPayCell.h"
#import "CommissionBankCell.h"
#import "CommissionAlipayModel.h"
#import "CommissionBankModel.h"

@interface CommissionPayModeController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;


@property(nonatomic,strong)CommissionAlipayModel *alipayModel;
@property(nonatomic,strong)CommissionBankModel *bankModel;


@end

@implementation CommissionPayModeController

static NSString *const CommissionPayModeReuseIdentifier = @"CommissionPayModeReuseIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buildView];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        CommissionAliPayCell *cell = [[CommissionAliPayCell alloc] initWithFrame:CGRectMake(0,0,WIDTH,70)];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = BACKGROUND_COLOR;
        
        if (_alipayModel) {
            cell.alipayName.text = self.alipayModel.alipay_realname;
            cell.alipayNumber.text = self.alipayModel.alipay_account;
            cell.bgView.backgroundColor = RGB(97,171,236);

        }else{
            
            cell.alipayName.text = @"支付宝暂未绑定";
            cell.alipayNumber.text = @"点击此处立刻绑定 ->";
        }
        
        
        return cell;
    }else{
        
        CommissionBankCell *cell=[tableView dequeueReusableCellWithIdentifier:CommissionPayModeReuseIdentifier];
        
        if (cell==nil) {
            cell=[[CommissionBankCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CommissionPayModeReuseIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = BACKGROUND_COLOR;
        
        if (_bankModel) {
            cell.bgView.backgroundColor = RGB(72,83,110);
            cell.bankNameLabel.text = self.bankModel.bank_name;
            cell.bankUserLabel.text = self.bankModel.realname;
            [cell.hidLabel setHidden:false];
            cell.bankNumberLabel.text = self.bankModel.card_no;
            [cell.bankLogoImg sd_setImageWithURL:[NSURL URLWithString:self.bankModel.bank_logo] placeholderImage:[UIImage imageNamed:@"icon_bank_default"]];
        }else{
            
            cell.bankNameLabel.text = @"银行暂未绑定,点击此处立刻绑定 ->";
            [cell.hidLabel setHidden:true];
            cell.bankLogoImg.image = [UIImage imageNamed:@"icon_bank_default"];
        }
        return cell;
    
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (_alipayModel) {
            
            if (self.alipayBlock) {
                self.alipayBlock(self.alipayModel);
            }
            [self.navigationController popViewControllerAnimated:true];
            
        }else{
            //跳转我的个人账户
            [self pushSetUserAccount];
        }
        
        
    }else{
        
        if (_bankModel) {
            
            if (self.bankBlock) {
                self.bankBlock(self.bankModel);
            }
            [self.navigationController popViewControllerAnimated:true];
            
        }else{
            //跳转我的个人账户
            [self pushSetUserAccount];

        }
        
    }
}
//
- (void)getCashData {

    WeakSelf(self);
    kW_DISPATCH_GLOBAL_QUEUE_DEFAULT(^{
    
        NSDictionary *params = @{@"username":USERNAME};
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Cash/info" completeBlock:^(id object) {
            
            StrongSelf(self);
            if (JudgeLogInData) {
                
                NSDictionary *dataDict = [object objectForKey:@"data"];
                
                kW_DISPATCH_MAIN_THREAD(^{
                    
                    id alipay = [dataDict objectForKey:@"alipay_info"];
                    if ([alipay isKindOfClass:[NSString class]]) {
                        NSLog(@"支付宝未绑定");
                        
                    }else{
                        NSLog(@"支付宝绑定");
                        self.alipayModel = [[CommissionAlipayModel alloc] initWithDictionary:alipay error:nil];
                        
                    }
                    
                    id bank = [dataDict objectForKey:@"bank_info"];
                    if ([bank isKindOfClass:[NSString class]]) {
                        NSLog(@"银行未绑定");
                        
                        
                    }else{
                        
                        NSLog(@"银行绑定");
                        self.bankModel = [[CommissionBankModel alloc] initWithDictionary:bank error:nil];
                    }
                    
                    [self.tableView reloadData];
                   
                });
            }
            
        }];
    
    });
    
}

- (void)pushSetUserAccount
{
    
    MyAccountTableViewController *myAccount = [[MyAccountTableViewController alloc] init];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:myAccount];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 70;
    }else{
        return 80;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}



- (void)buildView
{
    self.title = @"选择提现方式";
    self.view.backgroundColor = BACKGROUND_COLOR;
    
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"jiantou"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}



- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    //刷新绑定状态
    [self getCashData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    [self.navigationController.navigationBar setBarTintColor:DEFAULT_COLOR];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
